﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Yuebon.CMS.Application;
using Yuebon.CMS.Dtos;
using Yuebon.CMS.IServices;
using Yuebon.CMS.Models;
using Yuebon.Commons;
using Yuebon.Commons.Helpers;
using Yuebon.Commons.Models;
using Yuebon.WebApi.Areas.CMS.Models;
using Yuebon.WebApi.Controllers;
using Yuebon.WebApi.Models;

namespace Yuebon.WebApi.Areas.CMS.Controllers
{
    /// <summary>
    /// 文章管理
    /// </summary>
    [Route("api/CMS/[controller]")]
    [ApiController]
    public class ArticleNewsController : BusinessApiController<ArticleNews, IArticleNewsService>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_iService"></param>
        public ArticleNewsController(IArticleNewsService _iService) : base(_iService)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        protected override void OnBeforeInsert(ArticleNews info)
        {
            info.Id = GuidUtils.CreateNo();
            info.CreatorTime = DateTime.Now;
            info.CreatorUserId = CurrentUser.Id;
            info.DeleteMark = false;
            info.CompanyId = CurrentUser.OrganizeId;
            info.DeptId = CurrentUser.DepartmentId;
            if (info.SortCode == null)
            {
                info.SortCode = 99;
            }
        }

        /// <summary>
        /// 在更新数据前对数据的修改操作
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        protected override void OnBeforeUpdate(ArticleNews info)
        {
            info.LastModifyTime = DateTime.Now;
            info.LastModifyUserId = CurrentUser.Id;
            if (info.SortCode == null)
            {
                info.SortCode = 99;
            }
        }
        /// <summary>
        /// 根查询所有设置为幻灯的新闻
        /// </summary>
        /// <param name="token">token令牌</param>
        /// <returns>指定对象的集合</returns>
        [HttpGet("FindSlideArticle")]
        public IActionResult FindSlideArticle(string token)
        {
            //检查用户是否有权限，否则抛出MyDenyAccessException异常
            // CheckAuthorized(AuthorizeKey.ListKey);
            CommonResult result = new CommonResult();
            result = CheckToken(token);
            if (result.ErrCode == ErrCode.successCode)
            {
                string where = string.Format("IsSlide=1");
                IEnumerable<ArticleNews> list = iService.GetAllByIsNotDeleteAndEnabledMark(where).OrderBy(a=>a.SortCode);
                List<ArticleNewsDto> reslist = new List<ArticleNewsDto>();
                foreach(ArticleNews item in list)
                {
                    ArticleNewsDto articleNewsDto = new ArticleNewsDto();
                    articleNewsDto.Id = item.Id;
                    articleNewsDto.Title = item.Title;
                    articleNewsDto.ImgUrl = Configs.GetConfigurationValue("AppSetting", "FileUrl") + item.ImgUrl;
                    reslist.Add(articleNewsDto);
                }
                result.ResData = reslist;
            }
            return ToJsonContent(result);
        }

        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="info">请求参数</param>
        /// <returns>指定对象的集合</returns>
        [HttpPost("FindWithPagerFront")]
        public IActionResult FindWithPagerFront(ArticleNewsSearchRequest info)
        {
            //检查用户是否有权限，否则抛出MyDenyAccessException异常
            // CheckAuthorized(AuthorizeKey.ListKey);
            CommonResult result = new CommonResult();
            //result = CheckToken(token);
            //if (result.ErrCode == ErrCode.successCode)
            //{
                string keywords = info.Keywords;
                string orderByDir = info.Order;
                string orderFlied = info.Sort;
                string where = GetPagerCondition();
                bool order = orderByDir == "asc" ? false : true;
                if (!string.IsNullOrEmpty(keywords))
                {
                    where += string.Format(" and (Title like '%{0}%' or SeoTitle like '%{0}%' or Tags like '%{0}%' or Zhaiyao like '%{0}%')", keywords);
                }

                PagerInfo pagerInfo = new PagerInfo();
                pagerInfo.PageSize = info.PageSize;
                pagerInfo.CurrenetPageIndex = info.PageNo;
                ArticleNewsApp articleNewsApp = new ArticleNewsApp();
                List<ArticleNewsDto> list = articleNewsApp.FindWithPager(where, pagerInfo, orderFlied, order);
                result.ResData = new
                {
                    recordsTotal = pagerInfo.RecordCount,
                    recordsFiltered = pagerInfo.RecordCount,
                    data = list
                };
            //}
            return ToJsonContent(result);
        }
    }
}
